package com.xbh.management.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xbh.common.utils.JwtUtil;
import com.xbh.common.web.core.Result;
import com.xbh.management.model.ManSalaryGrade;
import com.xbh.management.service.SalaryGradeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: management-center
 * @description:
 * @author: 许宝华
 * @create: 2022-03-22 16:51
 */

@RestController
@RequestMapping(path = "/salaryGrade")
public class SalaryGradeController {

    @Autowired
    private SalaryGradeService salaryGradeService;

    /**
     *
     * @param params
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody JSONObject params){
        ManSalaryGrade manSalaryGrade = params.toJavaObject(ManSalaryGrade.class);
        manSalaryGrade.setCreatePerson(JwtUtil.getUsername());
        manSalaryGrade.setUpdatePerson(JwtUtil.getUsername());
        boolean save = salaryGradeService.save(manSalaryGrade);
        if (save) {
            return Result.success(manSalaryGrade);
        }
        return Result.failed("保存失败");
    }

    /**
     *
     * @param params
     * @return
     */
    @PostMapping("/page")
    public Result page(@RequestBody JSONObject params){
        int pageNum = params.getInteger("pageNum");
        int pageSize = params.getInteger("pageSize");
        LambdaQueryWrapper<ManSalaryGrade> wrapper = new LambdaQueryWrapper<>();
        if(StrUtil.isNotEmpty(params.getString("salaryGradeName"))){
            wrapper.like(ManSalaryGrade::getSalaryGradeName,params.getString("salaryGradeName"));
        }
        wrapper.orderByDesc(ManSalaryGrade::getCreateTime);
        Page<ManSalaryGrade> page = salaryGradeService.page(new Page<ManSalaryGrade>(pageNum, pageSize), wrapper);
        return Result.success(page);

    }
    /**
     *
     * @param params
     * @return
     */
    @PostMapping("/list")
    public Result list(@RequestBody JSONObject params){
        LambdaQueryWrapper<ManSalaryGrade> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ManSalaryGrade::getDeleted,0);
        if(StrUtil.isNotEmpty(params.getString("salaryGradeName"))){
            wrapper.like(ManSalaryGrade::getSalaryGradeName,params.getString("salaryGradeName"));
        }
        List<ManSalaryGrade> list = salaryGradeService.list(wrapper);
        return Result.success(list);

    }

    /**
     *
     * @param params
     * @return
     */
    @PostMapping("/update")
    public Result update(@RequestBody JSONObject params){

        ManSalaryGrade manSalaryGrade = params.toJavaObject(ManSalaryGrade.class);
        boolean update = salaryGradeService.updateById(manSalaryGrade);
        if (update) {
            return Result.success(manSalaryGrade);
        }
        return Result.failed("修改失败");


    }

    /**
     *
     * @param params
     * @return
     */
    @PostMapping("/delete")
    public Result delete(@RequestBody JSONObject params){
        ManSalaryGrade manSalaryGrade = params.toJavaObject(ManSalaryGrade.class);
        boolean delete = salaryGradeService.removeById(manSalaryGrade);
        if (delete) {
            return Result.success();
        }
        return Result.failed("删除失败");

    }

}
